<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ page isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <link href="${pageContext.request.contextPath}/css/style.css" rel="stylesheet" type="text/css" />
    <script type="javascript" src="${pageContext.request.contextPath}/layui/jquery2.1.1.js"></script>
    <script type=javascript src="${pageContext.request.contextPath}/layui/layui.js"></script>
    <script type=javascript src="${pageContext.request.contextPath}/layui/lay/modules/layer.js"></script>
    <link href="${pageContext.request.contextPath}/layui/css/layui.css"  media="all" rel="stylesheet" type="text/css" />
    <style type="text/css">
        #treeMenu input{
            display: -webkit-inline-flex;
        }
        #treeMenu li{
            display: inline-block;
            margin-left: 5px;
        }
        #ccc a{
            font-size: 16px;
        }
    </style>
</head>

<body>
<div class="place"> <span>位置：</span>
    <ul class="placeul">
        <li>系统管理</li>
        <li>职位管理</li>
        <li>赋权</li>
    </ul>
</div>
<div class="formbody">
    <div class="formtitle"><span>职位赋权</span></div>
</div>

<form class="layui-form" id="ccc" lay-filter="detail">

    <div class="layui-input-inline">
        <input type="hidden" id="id" name="id" >
    </div>
    <div class="layui-form-item" pane="">
    <ul id="treeMenu" class="forminfo">

    </ul>
        <%--<div class="layui-input-block">
            <input type="checkbox" name="like1[write]" lay-skin="primary" title="写作" checked="">
            <input type="checkbox" name="like1[read]" lay-skin="primary" title="阅读">
            <input type="checkbox" name="like1[game]" lay-skin="primary" title="游戏" disabled="">
        </div>--%>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn btn-edit" lay-submit="" lay-filter="savBtn">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script type="text/javascript">
    function show(id){
       /* $("#treeMenu").children("li").children(":checkbox").click(function(){
            var flag = $(this).attr("checked");
            $(this).siblings("ul").show();
            $(this).siblings("ul").children("li").children(":checkbox").attr("checked", flag);
        })*/
        console.log(id);
    }
</script>
<script src="https://www.layuicdn.com/layui-v2.5.6/layui.js"></script>
<script>
    layui.use(['form','layer','jquery'], function () {
        var form = layui.form
            ,layer = layui.layer
            ,$ = layui.jquery



        //赋值父模块
        $.ajax({
            url: "/module/getModuleByLevel1",
            type: "post",
            dataType: "json",
            success: function(data) {
                var datas = data.data;
                $('#treeMenu').empty();
                 for(var i=0;i<datas.length;i++){
                     $('#treeMenu').append('<li><input type="checkbox" id="'+datas[i].id+'"' +
                      ' onclick="show('+datas[i].id+')"  name="power" value="'+datas[i].id+'">  ' +
                         '<a href="javascript:void(0);">'+datas[i].mName+'</a>' +
                         /*'<div class="layui-input-block"></ul>' +*/
                         '<ul id=s'+datas[i].id+'>' +
                         /*'</div>' +*/
                         '</li>');
                 }
                form.render("select")//重新渲染 固定写法
            }
        });
        //赋值子模块
        $.ajax({
            url: "/module/getModuleByLevel2",
            type: "post",
            dataType: "json",
            success: function(data) {
                var datas2 = data.data;
                for(var i=0;i<datas2.length;i++){
                    $('#s'+datas2[i].pModule.id).append('<li><input type="checkbox" id="'+datas2[i].id+'"' +
                        ' onclick="show('+datas2[i].id+')"  name="power" value="'+datas2[i].id+'">  ' +
                        '<a href="javascript:void(0);">'+datas2[i].mName+'</a>' +
                        '</li>');
                }

                form.render("select")//重新渲染 固定写法
            }
        });











        form.on('submit(savBtn)', function(data){
            var roleId = $('#id').val();
            var moduleIds=[];
            $("input[name='power']:checked").each(function (){
                moduleIds.push($(this).val())
            });
            console.log("------------------xxxxxxxxxxxxxxxx")
            console.log(roleId)
            console.log(moduleIds)
            //向后台发送数据并进行添加操作
            $.ajax({
                url:"/role/addRoleAuth",
                type:"POST",
                traditional :true,
                /*contentType:"application/json",*/
                /*data: JSON.stringify(obj),*/
                data:{
                    roleId:roleId,
                    moduleIds:moduleIds
                },
                success:function(operate){
                    if(operate.message!="职位添加成功"){
                        layer.msg("添加成功!",{
                            icon:6,
                            time:500
                        });
                    }else{
                        layer.msg("赋权失败!");
                    }
                    form.render();
                }})
            return false;
        });
        form.render();
    })
</script>
</body>
</html>
